package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.util.AppCodeUtils
import com.polaris.live.databinding.WidgetMagicLampProgressBinding

/**
 * MagicLampProgressBar 周星进度条
 * @author Created by 天晴 on 2024/12/3 10:31
 * @since 1.0.0
 **/
class MagicLampProgressBar(context: Context, attributeSet: AttributeSet) :
    ConstraintLayout(context, attributeSet) {

    private val mBinding = WidgetMagicLampProgressBinding.inflate(LayoutInflater.from(context), this)

    private var max: Long = 100
    private var progress: Long = 0
    private val mPW: Int = 278.autoDp

    fun setMax(max: Long) {
        this.max = max
        updateView()

    }

    fun setProgress(progress: Long) {
        this.progress = progress
        updateView()
    }

    private fun updateView() {
        val ratio = (progress.toFloat() / max).coerceAtMost(0.99f)
        val ratioWidth = (ratio * mPW).toInt()

        (mBinding.progressFl.layoutParams as? MarginLayoutParams)?.apply {
            width = ratioWidth
            mBinding.progressFl.layoutParams = this
        }
        if (AppCodeUtils.isAR()) {
            (mBinding.progress.layoutParams as? MarginLayoutParams)?.apply {
                marginEnd = ratioWidth - mPW
                mBinding.progress.layoutParams = this
            }
            (mBinding.mask.layoutParams as? MarginLayoutParams)?.apply {
                marginEnd = ratioWidth - mPW
                mBinding.mask.layoutParams = this
            }
        }
    }

}